package com.itheima.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Comparator;

public class 对文本文件中的数字排序 {
    public static void main(String[] args) throws Exception{
        /*1.读取文本中的第一行数据*/
        BufferedReader br = new BufferedReader(new FileReader("IO02/xx.txt"));
        String line = br.readLine();

        /*2.使用空格切割*/
        String[] numbers = line.split(" ");
        System.out.println(Arrays.toString(numbers));

        /*3.对切割好的字符串数组排序*/
        Arrays.sort(numbers, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o1) - Integer.parseInt(o2);
            }
        });
        System.out.println(Arrays.toString(numbers));


        /*4.将排完序的字符串数组写入xxx.txt*/
        BufferedWriter bw = new BufferedWriter(new FileWriter("IO02/xxx.txt"));
        for (String number : numbers) {
            bw.write(number+" ");
        }


        br.close();
        bw.close();
    }
}
